package poi;

import javax.faces.context.FacesContext;
import java.util.Iterator;
import java.util.LinkedList;



public class POIEditor {

	private static final long serialVersionUID = -8779142511087199199L;
	private POIDatabase db = null;
	
	private LinkedList<GeoInfo> data = null;

	
	private int updLocationID;
	private double updLatitude;
	private double updLongitude;
	private String updLocationName;
	private String updLocationType;
	private String newRecord;
	private GlobalDataKeeper globalDK = null;
	private EditStateKeeper editSK = null;
	

	
	/**
	 * @return the searchResult
	 */
	/**
	 * @return the updLatitude
	 */
	public double getUpdLatitude() {
		return updLatitude;
	}

	/**
	 * @param updLatitude the updLatitude to set
	 */
	public void setUpdLatitude(double updLatitude) {
		this.updLatitude = updLatitude;
	}

	/**
	 * @return the updLongitude
	 */
	public double getUpdLongitude() {
		return updLongitude;
	}

	/**
	 * @param updLongitude the updLongitude to set
	 */
	public void setUpdLongitude(double updLongitude) {
		this.updLongitude = updLongitude;
	}

	/**
	 * @return the updLocationName
	 */
	public String getUpdLocationName() {
		return updLocationName;
	}

	/**
	 * @param updLocationName the updLocationName to set
	 */
	public void setUpdLocationName(String updLocationName) {
		this.updLocationName = updLocationName;
	}

	/**
	 * @return the updLocationType
	 */
	public String getUpdLocationType() {
		return updLocationType;
	}

	/**
	 * @param updLocationType the updLocationType to set
	 */
	public void setUpdLocationType(String updLocationType) {
		this.updLocationType = updLocationType;
	}

	/**
	 * @return the updLocationID
	 */
	public int getUpdLocationID() {
		return updLocationID;
	}

	/**
	 * @return the newRecord
	 */
	public String getNewRecord() {
		return newRecord;
	}

	/**
	 * @param newRecord the newRecord to set
	 */
	public void setNewRecord(String newRecord) {
		this.newRecord = newRecord;
	}


	/**
	 * @return the data
	 */
	public LinkedList<GeoInfo> getData() {
		return data;
	}

	public void setUpdLocationID(int updLocationID) {
		this.updLocationID = updLocationID;
	}	
	
	private void resetUpdFrm() {
		if (editSK.getUpdLocationID() == 0) {
			updLocationID  = db.getNewID();
			newRecord = "Add";
		} else {
			updLocationID = editSK.getUpdLocationID();
			newRecord = "Update";
		}
		updLatitude = 0;
		updLongitude = 0;
		updLocationName = "";
		updLocationType = "My";
	}
	
	@SuppressWarnings("deprecation")
	private void initObject(String csvFileName) {
		LogFormatter.setLogFormat();
		FacesContext facesContext = FacesContext.getCurrentInstance();
		globalDK = (GlobalDataKeeper) facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, "globalDataKeeper");
		editSK = (EditStateKeeper) facesContext.getApplication().getVariableResolver().resolveVariable(facesContext, "editStateKeeper");
		db = globalDK.getData();
		data = db.buildList(null);
		
		int r = getURLParam();
		if (r > 0) {
			GeoInfo val = db.getItemByID(r);
			if (val != null) {
				updLocationID = val.getPointID();
				updLatitude = val.getLatitude();
				updLongitude = val.getLongtitude();
				updLocationName = val.getLocationName();
				updLocationType = val.getLocationTypeName();
				newRecord = "Update";
				
				editSK.setUpdLocationID(updLocationID);
			} else {
				resetUpdFrm();
			}
		} else {
			resetUpdFrm();
		}
	}
	
	public POIEditor() {
		initObject("POISystem\\poidata.csv");
	}

	public POIEditor(int r) {
		LogFormatter.setLogFormat();
		globalDK = null;
		editSK = new EditStateKeeper();
		db = new POIDatabase();
		db.connectSource(null);
		data = db.buildList(null);
		if (r > 0) {
			GeoInfo val = db.getItemByID(r);
			if (val != null) {
				updLocationID = val.getPointID();
				updLatitude = val.getLatitude();
				updLongitude = val.getLongtitude();
				updLocationName = val.getLocationName();
				updLocationType = val.getLocationTypeName();
				newRecord = "Update";
				editSK.setUpdLocationID(updLocationID);
			} else {
				resetUpdFrm();
			}
		} else {
			resetUpdFrm();
		}

	}


	public String addItem() {
		if (updLocationName.length() == 0) {
			return "Error";
		}
		GeoInfo val = new GeoInfo(updLocationID,updLocationName,updLocationType,updLatitude,updLongitude); 
		db.add(val);
		if(globalDK != null)globalDK.saveData();
		if (newRecord.equals("Add")) {
			resetUpdFrm();
		}
		editSK.setUpdLocationID(0);
		return "Done!";	
	}

	
	public int getURLParam() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		if (facesContext == null) {
			return -1;
		}
		String param = (String) facesContext.getExternalContext().getRequestParameterMap().get("itemID");
		if (param == null) {
			return -1;
		}
		int r = Integer.parseInt(param);
		return r; 
	}
	
	protected void finalize() {
		
	}

}
